package org.example.service.impl;

import org.example.dao.CodeRepository;
import org.example.model.Code;
import org.example.service.CodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CodeServiceImpl implements CodeService {

    @Autowired
    private CodeRepository codeRepository;

    public void addCode(Code code){
        codeRepository.save(code);
    }

    @Override
    public List<Code> findByEmailOrderByCreatetimeDesc(Code code) {
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("email", ExampleMatcher.GenericPropertyMatchers.exact());
        Sort sort = new Sort(Sort.Direction.DESC,"createtime");
        return codeRepository.findAll(Example.of(code, matcher), sort);
    }
}
